package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import com.fastadmin.common.exception.EntityExistsException;
import com.fastadmin.pojo.History;
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.core.exception.JdbcDataException;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xphsc.easyjdbc.core.metadata.ElementResolver;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;

@Repository
@Slf4j
public class HistoryMapper extends BaseMapper {
    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public PageInfo<History> findByFastParam(FastParam fastParam) {
        String tableName = ElementResolver.resolve(History.class).getTable();
        PageInfo<History> pageInfo = this.easyJdbcTemplate.findByPage(fastParam.getSql(tableName), History.class, fastParam.getOffset(), fastParam.getLimit());
        return pageInfo;
    }

    public History findById(Long ids) {
        History history = this.easyJdbcTemplate.getByPrimaryKey(History.class, ids);
        return history;
    }

    public void deleteById(String id) {
        this.easyJdbcTemplate.deleteByPrimaryKey(History.class, id);
    }

    public void update(History history) {
        history.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        this.easyJdbcTemplate.update(history);
    }

    public Object save(History history) throws EntityExistsException {
        history.setCreate_time(new Timestamp(System.currentTimeMillis()));
        history.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        try {
            return this.easyJdbcTemplate.insertKey(history);
        } catch (Exception e) {
            log.debug("莫的事，重复浏览了{}");
            return null;
        }

    }

    public List<History> findByIds(String ids) {
        if (StrUtil.isBlank(ids)) {
            return CollUtil.newArrayList();
        }
        return this.easyJdbcTemplate.findByIds(History.class, Arrays.asList(StrUtil.splitToArray(ids, ',')));

    }
}
